package com.college.collegeinfomanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.collegeinfomanage.dto.CampusLifeDTO;
import com.college.collegeinfomanage.entity.CampusLife;
import com.college.collegeinfomanage.mapper.CampusLifeMapper;
import com.college.collegeinfomanage.mapper.UniversityMapper;
import com.college.collegeinfomanage.service.CampusLifeService;
import com.college.collegeinfomanage.vo.CampusLifeListVO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CampusLifeServiceImpl extends ServiceImpl<CampusLifeMapper, CampusLife> implements CampusLifeService {

    @Resource
    private CampusLifeMapper campusLifeMapper;

    @Resource
    private UniversityMapper universityMapper;

    @Override
    public IPage<CampusLifeListVO> getPage(CampusLifeDTO campusLifeDTO) {
        LambdaQueryWrapper<CampusLife> queryWrapper = new LambdaQueryWrapper<>();
        if (campusLifeDTO.getUniversityId() != null) {
            queryWrapper.eq(CampusLife::getUniversityId, campusLifeDTO.getUniversityId());
        }
        IPage<CampusLife> page = new Page<>(campusLifeDTO.getPage(), campusLifeDTO.getSize());
        IPage<CampusLife> campusLifeIPage = campusLifeMapper.selectPage(page, queryWrapper);
        List<CampusLife> records = campusLifeIPage.getRecords();
        List<CampusLifeListVO> collect1 = records.stream()
                .map(campusLife -> {
                    CampusLifeListVO campusLifeListVO = new CampusLifeListVO();
                    campusLifeListVO.setId(campusLife.getId());
                    campusLifeListVO.setUniversityId(campusLife.getUniversityId());
                    campusLifeListVO.setUniversityName(universityMapper.selectById(campusLife.getUniversityId()).getName());
                    campusLifeListVO.setCampusEnvironment(campusLife.getCampusEnvironment());
                    campusLifeListVO.setDormitory(campusLife.getDormitory());
                    campusLifeListVO.setStudentClubs(campusLife.getStudentClubs());
                    return campusLifeListVO;
                })
                .collect(Collectors.toList());
        IPage<CampusLifeListVO> page1 = new Page<>(campusLifeDTO.getPage(), campusLifeDTO.getSize());
        page1.setTotal(campusLifeIPage.getTotal());
        page1.setPages(campusLifeIPage.getPages());
        page1.setCurrent(campusLifeIPage.getCurrent());
        page1.setSize(campusLifeIPage.getSize());
        page1.setRecords(collect1);
        return page1;
    }
}
